package com.ynt.pay.common.dto;

import com.ynt.pay.common.enums.ExceptionCodes;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * 返回结果集对象
 * Created by gan on 2019/10/6 10:20.
 */
@Data
public class ResponseData<T> {

    private Boolean success = true;
    private String code;
    private String message;
    private T data;


    public ResponseData(boolean success, String code, String message, T date) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = date;
    }

    public ResponseData() {

    }

    public static <T> com.ynt.pay.common.utils.ResponseData<T> success(T data) {
        return new com.ynt.pay.common.utils.ResponseData(true, ExceptionCodes.SUCCESS.getCode(), ExceptionCodes.SUCCESS.getDesc(), data);
    }

    public static <T> com.ynt.pay.common.utils.ResponseData<T> success(String message, T data) {
        return new com.ynt.pay.common.utils.ResponseData(true, ExceptionCodes.SUCCESS.getCode(), message, data);
    }

    public static <T> com.ynt.pay.common.utils.ResponseData<T> success() {
        return new com.ynt.pay.common.utils.ResponseData(true, ExceptionCodes.SUCCESS.getCode(), ExceptionCodes.SUCCESS.getDesc(), null);
    }

    public static <T> com.ynt.pay.common.utils.ResponseData<T> error(String code, String message) {
        if (StringUtils.isBlank(message)) message = ExceptionCodes.SYSTEM_EXCEPTION.getCode();
        return new com.ynt.pay.common.utils.ResponseData(false, code, message, null);
    }

    public static <T> com.ynt.pay.common.utils.ResponseData<T> error(String code, String message, T data) {
        if (StringUtils.isBlank(message)) message = ExceptionCodes.SYSTEM_EXCEPTION.getCode();
        return new com.ynt.pay.common.utils.ResponseData(false, code, message, data);
    }

    public static <T> com.ynt.pay.common.utils.ResponseData<T> build(boolean success, ExceptionCodes e, T data) {
        return new com.ynt.pay.common.utils.ResponseData(success, e.getCode(), e.getDesc(), data);
    }


}

